<?php

/**
 * SigmaCMS - Content Management System
 *
 * Copyright (C) 2008 Pavol Biely
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    SigmaCMS
 * @author     Pavol Biely <pavolbiely@gmail.com>
 * @copyright  2008 Pavol Biely
 * @license    http://www.gnu.org/licenses/gpl.txt   GNU General Public License
 * @link       http://pabi3.com/
 * @since      0.1.0
 */

final class Plugin
{
	/**
	 * Plugin objects
	 *
	 * @var array
	 */
	static private $_plugin = array();

	/**
	 * Register all plugins
	 *
	 * @return void
	 */
	static public function registerAll()
	{
		$sql = db::query("SELECT `id`,`name` FROM ".db::Prefix('plugins')." WHERE `active`='1' ORDER BY `priority` DESC");
		while ($row = db::fetchObject($sql)) {
			self::register($row->id);
		}
		db::free($sql);
	}

	/**
	 * Unregister all plugins
	 *
	 * @return void
	 */
	static public function unregisterAll()
	{
		self::$_plugin = array();
	}

	/**
	 * Register a new plugin
	 *
	 * @param string $id
	 * @return void
	 */
	static public function register($id)
	{
		// is the plugin ID valid?
		if (!ctype_alnum($id)) return;

		// if the plugin is already registered, nothing will happen
		if (self::isRegistered($id)) return;

		// convert to lowercase
		$id = strtolower($id);

		// relative plugin path
		$rPath = cfg::DIR_PLUGINS.cfg::PLUGIN_PREFIX.$id.'.php';

		// absolute plugin path
		$path = dirname(__FILE__).'/../'.$rPath;

		// does the file exist?
		if (is_file($path)) {
			// include the plugin file
			include_once $path;

			// name of the plugin class
			$className = 'Plugin_'.ucfirst($id);

			// does the class exist?
			if (class_exists($className)) {
				// create a new object
				self::$_plugin[$id] = new $className();

				// is the class a child of the PluginAPI interface?
				if (!self::$_plugin[$id] instanceof PluginAPI) {
					self::unregister($id);
				}
			}
		} else {
			Actionlog::add(sprintf(_LOG_PLUGIN_SCRIPTS_NOT_FOUND, $id, $rPath));
		}
	}

	/**
	 * Unregister the plugin
	 *
	 * @param string $id
	 * @return void
	 */
	static public function unregister($id)
	{
		unset(self::$_plugin[strtolower($id)]);
	}
	
	/**
	 * Check if the plugin is already registered
	 *
	 * @param string $id
	 * @return boolean
	 */
	static public function isRegistered($id)
	{
		if (array_key_exists(strtolower($id),self::$_plugin)) {
			return true;
		}
		return false;
	}

	/**
	 * Check if the plugin is already installed
	 *
	 * @param string $id
	 * @return boolean
	 */
	static public function isInstalled($id)
	{
		// cache list of installed plugins
		static $installed = array();

		// is requested plugin in the cache?
		if (isset($installed[$id])) {
			return true;
		}

		// load installed plugins
		$sql = db::query("SELECT `id` FROM ".db::prefix('plugins'));
		while ($row = db::fetchObject($sql)) {
			$installed[$row->id] = true;
		}
		db::free($sql);

		return isset($installed[$id]) ? true : false;
	}

	/**
	 * Get the plugin instance
	 *
	 * @param string $id
	 * @return mixed
	 */
	static public function getPlugin($id)
	{
		if (self::isRegistered($id)) {
			return self::$_plugin[$id];
		}
		return false;
	}

	/**
	 * Get instances of all plugins in an array
	 *
	 * @return array
	 */
	static public function getPlugins()
	{
		return (array) self::$_plugin;
	}
}
